package com.utils.sm;

import cn.hutool.crypto.CryptoException;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.SM4;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.Security;

/**
 * @author SZYYYFPT003
 * @data 2023/02/07
 */
public class Sm4Utils {
    private static final Logger logger = LoggerFactory.getLogger(Sm4Utils.class);
    private static final byte[]  ACOMMONKU =        "wertgstsgstshsys".getBytes();
    private static final byte[]  ACOMMONKUPIANYI =  "werdfgshfwwghsys".getBytes();

    static{
            Security.addProvider(new BouncyCastleProvider());
    }
    private Sm4Utils() {
    }

    public static String encrypt(String string){
        try {
            if(StringUtils.isNotEmpty(string)){
                return new SM4(Mode.CBC, Padding.PKCS5Padding,ACOMMONKU,ACOMMONKUPIANYI).encryptHex(string);
            }
        }catch (CryptoException e) {
            logger.error("异常{}", e.getCause());
        }
        return "";
    }
    public static String decrypt(String string){
        try {
            if(StringUtils.isNotEmpty(string)){
                return new SM4(Mode.CBC, Padding.PKCS5Padding,ACOMMONKU,ACOMMONKUPIANYI).decryptStr(string);
            }
        }catch (CryptoException e) {
            logger.error("异常{}", e.getCause());
        }
        return "";
    }



}
